{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# stable_marriage"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/stable_marriage.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/stable_marriage.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Stable marriage problem in Google CP Solver.\n",
    "\n",
    "  Problem and OPL model from Pascal Van Hentenryck\n",
    "  'The OPL Optimization Programming Language', page 43ff.\n",
    "\n",
    "  Also, see\n",
    "  http://www.comp.rgu.ac.uk/staff/ha/ZCSP/additional_problems/stable_marriage/stable_marriage.pdf\n",
    "\n",
    "  Note: This model is translated from my Comet model\n",
    "       http://www.hakank.org/comet/stable_marriage.co\n",
    "  I have kept some of the constraint from that code.\n",
    "\n",
    "  Compare with the following models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/stable_marriage.mzn\n",
    "  * Comet   : http://www.hakank.org/comet/stable_marriage.co\n",
    "  * ECLiPSe : http://www.hakank.org/eclipse/stable_marriage.ecl\n",
    "  * Gecode  : http://hakank.org/gecode/stable_marriage.cpp\n",
    "  * SICStus : http://hakank.org/sicstus/stable_marriage.pl\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def main(ranks, problem_name):\n",
    "\n",
    "  # Create the solver\n",
    "  solver = pywrapcp.Solver(\"Stable marriage\")\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  print(\"Problem name:\", problem_name)\n",
    "\n",
    "  rankMen = ranks[\"rankMen\"]\n",
    "  rankWomen = ranks[\"rankWomen\"]\n",
    "\n",
    "  n = len(rankMen)\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  wife = [solver.IntVar(0, n - 1, \"wife[%i]\" % i) for i in range(n)]\n",
    "  husband = [solver.IntVar(0, n - 1, \"husband[%i]\" % i) for i in range(n)]\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "\n",
    "  # forall(m in Men)\n",
    "  #    cp.post(husband[wife[m]] == m);\n",
    "  for m in range(n):\n",
    "    solver.Add(solver.Element(husband, wife[m]) == m)\n",
    "\n",
    "  # forall(w in Women)\n",
    "  #    cp.post(wife[husband[w]] == w);\n",
    "  for w in range(n):\n",
    "    solver.Add(solver.Element(wife, husband[w]) == w)\n",
    "\n",
    "  # forall(m in Men, o in Women)\n",
    "  # cp.post(rankMen[m,o] < rankMen[m, wife[m]] => rankWomen[o,husband[o]] <\n",
    "  # rankWomen[o,m]);\n",
    "  for m in range(n):\n",
    "    for o in range(n):\n",
    "      b1 = solver.IsGreaterCstVar(\n",
    "          solver.Element(rankMen[m], wife[m]), rankMen[m][o])\n",
    "      b2 = (\n",
    "          solver.IsLessCstVar(\n",
    "              solver.Element(rankWomen[o], husband[o]), rankWomen[o][m]))\n",
    "      solver.Add(b1 - b2 <= 0)\n",
    "\n",
    "  # forall(w in Women, o in Men)\n",
    "  # cp.post(rankWomen[w,o] < rankWomen[w,husband[w]] => rankMen[o,wife[o]] <\n",
    "  # rankMen[o,w]);\n",
    "  for w in range(n):\n",
    "    for o in range(n):\n",
    "      b1 = solver.IsGreaterCstVar(\n",
    "          solver.Element(rankWomen[w], husband[w]), rankWomen[w][o])\n",
    "      b2 = solver.IsLessCstVar(\n",
    "          solver.Element(rankMen[o], wife[o]), rankMen[o][w])\n",
    "      solver.Add(b1 - b2 <= 0)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  solution = solver.Assignment()\n",
    "  solution.Add(wife)\n",
    "  solution.Add(husband)\n",
    "\n",
    "  db = solver.Phase(wife + husband, solver.CHOOSE_FIRST_UNBOUND,\n",
    "                    solver.ASSIGN_MIN_VALUE)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "  num_solutions = 0\n",
    "  solutions = []\n",
    "  while solver.NextSolution():\n",
    "    # solutions.append([x[i].Value() for i in range(x_len)])\n",
    "    print(\"wife   : \", [wife[i].Value() for i in range(n)])\n",
    "    print(\"husband: \", [husband[i].Value() for i in range(n)])\n",
    "    print()\n",
    "    num_solutions += 1\n",
    "\n",
    "  solver.EndSearch()\n",
    "\n",
    "  print()\n",
    "  print(\"num_solutions:\", num_solutions)\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "  print(\"#############\")\n",
    "  print()\n",
    "\n",
    "\n",
    "#\n",
    "# From Van Hentenryck's OPL book\n",
    "#\n",
    "van_hentenryck = {\n",
    "    \"rankWomen\": [[1, 2, 4, 3, 5], [3, 5, 1, 2, 4], [5, 4, 2, 1, 3],\n",
    "                  [1, 3, 5, 4, 2], [4, 2, 3, 5, 1]],\n",
    "    \"rankMen\": [[5, 1, 2, 4, 3], [4, 1, 3, 2, 5], [5, 3, 2, 4, 1],\n",
    "                [1, 5, 4, 3, 2], [4, 3, 2, 1, 5]]\n",
    "}\n",
    "\n",
    "#\n",
    "# Data from MathWorld\n",
    "# http://mathworld.wolfram.com/StableMarriageProblem.html\n",
    "#\n",
    "mathworld = {\n",
    "    \"rankWomen\": [[3, 1, 5, 2, 8, 7, 6, 9, 4], [9, 4, 8, 1, 7, 6, 3, 2, 5],\n",
    "                  [3, 1, 8, 9, 5, 4, 2, 6, 7], [8, 7, 5, 3, 2, 6, 4, 9, 1],\n",
    "                  [6, 9, 2, 5, 1, 4, 7, 3, 8], [2, 4, 5, 1, 6, 8, 3, 9, 7],\n",
    "                  [9, 3, 8, 2, 7, 5, 4, 6, 1], [6, 3, 2, 1, 8, 4, 5, 9, 7],\n",
    "                  [8, 2, 6, 4, 9, 1, 3, 7, 5]],\n",
    "    \"rankMen\": [[7, 3, 8, 9, 6, 4, 2, 1, 5], [5, 4, 8, 3, 1, 2, 6, 7, 9],\n",
    "                [4, 8, 3, 9, 7, 5, 6, 1, 2], [9, 7, 4, 2, 5, 8, 3, 1, 6],\n",
    "                [2, 6, 4, 9, 8, 7, 5, 1, 3], [2, 7, 8, 6, 5, 3, 4, 1, 9],\n",
    "                [1, 6, 2, 3, 8, 5, 4, 9, 7], [5, 6, 9, 1, 2, 8, 4, 3, 7],\n",
    "                [6, 1, 4, 7, 5, 8, 3, 9, 2]]\n",
    "}\n",
    "\n",
    "#\n",
    "# Data from\n",
    "# http://www.csee.wvu.edu/~ksmani/courses/fa01/random/lecnotes/lecture5.pdf\n",
    "#\n",
    "problem3 = {\n",
    "    \"rankWomen\": [[1, 2, 3, 4], [4, 3, 2, 1], [1, 2, 3, 4], [3, 4, 1, 2]],\n",
    "    \"rankMen\": [[1, 2, 3, 4], [2, 1, 3, 4], [1, 4, 3, 2], [4, 3, 1, 2]]\n",
    "}\n",
    "\n",
    "#\n",
    "# Data from\n",
    "# http://www.comp.rgu.ac.uk/staff/ha/ZCSP/additional_problems/stable_marriage/stable_marriage.pdf\n",
    "# page 4\n",
    "#\n",
    "problem4 = {\n",
    "    \"rankWomen\": [[1, 5, 4, 6, 2, 3], [4, 1, 5, 2, 6, 3], [6, 4, 2, 1, 5, 3],\n",
    "                  [1, 5, 2, 4, 3, 6], [4, 2, 1, 5, 6, 3], [2, 6, 3, 5, 1, 4]],\n",
    "    \"rankMen\": [[1, 4, 2, 5, 6, 3], [3, 4, 6, 1, 5, 2], [1, 6, 4, 2, 3, 5],\n",
    "                [6, 5, 3, 4, 2, 1], [3, 1, 2, 4, 5, 6], [2, 3, 1, 6, 5, 4]]\n",
    "}\n",
    "\n",
    "main(van_hentenryck, \"Van Hentenryck\")\n",
    "main(mathworld, \"MathWorld\")\n",
    "main(problem3, \"Problem 3\")\n",
    "main(problem4, \"Problem4\")\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
